package com.sayimo.maker.commons.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * @Project: sayimoWeixinApi
 * @ClassName: ImageUtil
 * @Description: 图片工具类
 * @Author:	王瑞清
 * @Date:	日期：2015年9月8日  时间：下午5:36:00
 * @Version 1.0.0
 */
public class ImageUtil {

	private static Log logger = LogFactory.getLog(ImageUtil.class);
	/** 
	* @Title: unionImage 
	* @Description: 合成图片
	* @param bgImagePath
	* @param smallImagePath
	* @return boolean
	*/ 
	public static void unionImage(Integer x,Integer y,String bgImagePath, String smallImagePath,OutputStream outputStream) {
		try {
			InputStream is = new FileInputStream(bgImagePath);
			JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
			BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
			Graphics g = buffImg.getGraphics();
			ImageIcon imgIcon = new ImageIcon(smallImagePath);
			Image img = imgIcon.getImage();
			g.drawImage(img, x, y, null);
			JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(outputStream);
			en.encode(buffImg);
			is.close();
			outputStream.close();
		} catch (IOException ie) {
			logger.debug(ie);
		}
	}
	
	
	/** 
	* @Title: imageWriteWord 
	* @Description: 图片上写文字
	* @param x
	* @param y
	* @param bgImagePath
	* @param word
	* @param outputStream
	* @return void
	*/ 
	public static void imageWriteWord(Integer x,Integer y,String bgImagePath,String word,OutputStream outputStream){
		try {
	        InputStream is = new FileInputStream(bgImagePath);
	        JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
	        BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
	        Graphics g = buffImg.getGraphics();
	        g.setColor(Color.BLACK);
	        Font f = new Font("宋体",Font.BOLD,30);
	        g.setFont(f);
	        g.drawString(word,10,30);
	        g.dispose();
	        JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(outputStream);
	        en.encode(buffImg);
	        is.close();
	        outputStream.close();
		} catch (IOException ie) {
			logger.debug(ie);
		}
	}
	
	
	/** 
	* @Title: resizeImage 
	* @Description:需要缩小的倍数，缩小2倍为原来的1/2 ，这个数值越大，返回的图片越小 
	* @param filePath
	* @param resizeTimes
	* @return BufferedImage
	*/ 
	public static BufferedImage resizeImage(String filePath,float resizeTimes){
        InputStream is = null;
        BufferedImage buffImage  = null;
		try {
		is = new FileInputStream(filePath);
        JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
        BufferedImage matrixIamge;
		matrixIamge = jpegDecoder.decodeAsBufferedImage();
		int width = matrixIamge.getWidth();  
        int height = matrixIamge.getHeight();  
        int toWidth = (int) (Float.parseFloat(String.valueOf(width)) * resizeTimes);  
        int toHeight = (int) (Float.parseFloat(String.valueOf(height)) * resizeTimes);  
        buffImage = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);  
        buffImage.getGraphics().drawImage(matrixIamge.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null); 
		}catch (FileNotFoundException e) {
			logger.debug(e);
		}catch (ImageFormatException e) {
			logger.debug(e);
		} catch (IOException e) {
			logger.debug(e);
		}
		return buffImage;
		}
	
	
	/** 
	* @Title: convertJpg 
	* @Description: 转JPG
	* @return void
	*/ 
	public static void convertJpg(String filePath) throws IOException{
			InputStream is = new FileInputStream(filePath);
			BufferedImage im = ImageIO.read(is);
			File file = new File(filePath);
			ImageIO.write(im, "jpg",file);
	}
		
}
